package com.形为型模式.状态模式;

/**
 * 抽象状态类
 */
abstract class AbstractState{
    protected ScoreContent hj;
    protected String stateName;
    protected int score;
    abstract void checkState();
    void addScore(int x){
      score += x;
        System.out.println(String.format("加上：%s分，当前分数是:%s",x,score));
        checkState();
        System.out.println(String.format("当前状态：%s",hj.getState().stateName));
    }
}

/**
 * 分数环境类
 */
class ScoreContent{
    private AbstractState state;
    ScoreContent(){
        state = new LowState(this);
    }
    void add(int score){
        state.addScore(score);
    }

    public AbstractState getState() {
        return state;
    }

    public void setState(AbstractState state) {
        this.state = state;
    }
}

/**
 * 优秀 具体状态
 */
class HighState extends AbstractState{
    @Override
    void checkState() {
        if(score < 60){
            hj.setState(new LowState(this));
        }
        else if(score < 90){
            hj.setState(new MiddleState(this));
        }

    }
   HighState(AbstractState state){
      hj = state.hj;
      stateName = "优秀";
      score = state.score;
   }
}

/**
 * 中等 具体状态
 */
class MiddleState extends AbstractState{
    @Override
    void checkState() {
       if(score < 60){
           hj.setState(new LowState(this));
       }
       else if(score >= 90){
           hj.setState(new HighState(this));
       }
    }
    MiddleState(AbstractState state){
        hj = state.hj;
        stateName = "中等";
        score = state.score;
    }
}

/**
 * 不及格 具体状态
 */
class LowState extends AbstractState{
    public LowState(AbstractState state) {
       hj = state.hj;
       stateName = "不及格";
       score = state.score;
    }
    LowState(ScoreContent content){
        hj = content;
        stateName  = "不及格";
        score = 0;
    }
    @Override
    void checkState() {
        if(score >= 90){
            hj.setState(new HighState(this));
        }
        else if(score >= 60){
            hj.setState(new MiddleState(this));
        }

    }
}
/**
 * 学生成绩的状态模式
 */
public class StateScore {
    public static void main(String[] args) {
        ScoreContent content = new ScoreContent();
       content.add(90);
    }
}
